<?php
class manageController extends RPHP_Controller
{
    //管理系统面板
    public function index()
    {

        $model = $this->model;
        $auth  = $model->checkAuth();
        if ($auth) {
            $this->view->auth = $auth;

            $bindUid = $model->getBindUid();

            // 检查是否有概览统计权限
            if ($model->checkPriv(array("analysis_hm"), false, false)) {

                $this->view->chart   = $model->getHomeChartData();
                $this->view->goods   = $model->getGoodsCount();
                $this->view->order   = $model->getOrderCount();
                $this->view->member  = $model->getMemberCount();
                $this->view->paidnum = $model->getPaidNum();
                $this->view->cart    = $model->getCartCount();
                $this->display();
            } elseif ($bindUid) {
                $this->view->stock      = $model->getStockOut($bindUid);
                $this->view->moneytoday = $model->getSeparateToday($bindUid);
                // print_r($this->view->stock);
                $time_start = strtotime(date("Y-m-1 0:0:0"));
                $time_end   = $_SERVER['REQUEST_TIME'];

                $year   = date("Y");
                $month  = date("n");
                $maxday = $this->c['month'][$month];
                if ($month == 2 && (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0)) {
                    $maxday = 29;
                }

                $this->view->chart = array(
                    'date'   => date("Y年n月", $time_start),
                    'start'  => date("j日", $time_start),
                    'end'    => date("j日", $time_end),
                    'eofday' => date("j", $time_end),
                    'maxday' => $maxday,
                );

                $this->view->user = $model->getUserChart($bindUid);

                $this->display('bind_dashaboard');
            }
            // 查看默认登录信息
            else {
                $log                  = $model->getLastSign($auth['aid']);
                $this->view->log_time = $log['log_time'];
                $this->view->log_ip   = $log['log_ip'];
                $this->display('index_dashaboard');
            }

        }

    }

    //退出登录
    public function signout()
    {
        $model = $this->model;
        $auth  = $model->checkAuth(true, false);
        if ($auth) {
            $model->deleteOnline($auth['token']);
            //送出COOKIE
            setcookie($this->c['cookie']['user'], "", 0, $this->c['cookie']['path'], $this->c['cookie']['host']);
            header("location: /");
        }
    }

    //修改密码表单
    public function password()
    {
        $model = $this->model;
        $auth  = $model->checkAuth();
        if ($auth) {
            $this->view->auth = $auth;
            $this->display();

        }

    }

    //执行修改密码
    public function savepassword()
    {
        $code  = 900;
        $model = $this->model;
        $auth  = $model->checkAuth();
        if ($auth) {
            $this->view->auth = $auth;
            if ($_POST['act'] == "mod") {

                $this->view->success = "修改密码成功";
                $this->view->link    = array(
                    array('name' => '返回管理后台', 'url' => '/manage'),
                );

                $code = $model->savePassword($_POST['cur_pass'], $_POST['new_pass']);
            }

        }
        $this->view->code = $code;
        $this->displaydo();
    }

}
